package org.netty.push.core.proto.codec;

import org.netty.push.core.proto.BaseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class CustomerEncoder extends MessageToByteEncoder<BaseMessage> {
	private static  Logger logger=LoggerFactory.getLogger(CustomerDecoder.class);

	@Override
	protected void encode(ChannelHandlerContext ctx, BaseMessage msg, ByteBuf out) throws Exception {
		logger.debug("CustomerEncoder.................");
		ObjectMapper objectMapper=new ObjectMapper();
		String objStr=objectMapper.writeValueAsString(msg);
		
//		System.err.println("objStr:"+objStr);
//		System.err.println("msg.getClass().getName():"+msg.getClass().getName());
		
		out.writeInt(msg.getClass().getName().getBytes().length);
		out.writeBytes(msg.getClass().getName().getBytes());
		

		out.writeInt(objStr.getBytes().length);
		out.writeBytes(objStr.getBytes());
		
		
	}

}
